package com.thoughtworks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class LibraryManager {

    public String libraryNumber = null;
    Library library;
    ArrayList<Book> book;
    ArrayList<Movie> movie;

    private final HashMap<String, String> login;
    public boolean isLoggedIn;
    private HashMap<Choices, Action> actionMap;
    public UserDetails userDetails=new UserDetails();

    public enum Choices {NOTHING, SHOW_PROFILE, LIST_CATALOG,  CHECKOUT_BOOK, RETURN_BOOK, CHECKOUT_MOVIE, RETURN_MOVIE, LOGOUT, QUIT};
    public Input input = new Input();
    public Output output = new Output();

    public LibraryManager() {
        book = new ArrayList<Book>();
        book.add(new Book("Java Puzzlers", "Joshua Bloch", 2005));
        book.add(new Book("Python:The Complete Reference", "Martin C. Brown", 2001));
        book.add(new Book("Two States", "Chetan Bhagat", 2014));
        book.add(new Book("Success Series", "Sachchida Nand Jha", 2013));
        book.add(new Book("Frankenstein", "Mary Shelley", 1998));


        movie = new ArrayList<Movie>();
        movie.add(new Movie("Gilli", "Dharani", 2004, 9));
        movie.add(new Movie("Veeram", "Siva", 2014, 8));
        movie.add(new Movie("Karate Kid", "Harald Zwart", 2010, 9));

        login = new HashMap<String, String>();
        login.put("123-0001", "abc");
        login.put("123-0002", "def");
        login.put("123-0003", "ghi");
        this.isLoggedIn = false;

        this.library = new Library(book, movie);
        actionMap = new HashMap<Choices,Action>();
//        actionMap.put(Choices.SHOW_PROFILE, "ShowProfileAction");
//        actionMap.put(Choices.LIST_CATALOG, "ListCatalogAction");
//        actionMap.put(Choices.CHECKOUT_BOOK, "CheckOutBookAction");
//        actionMap.put(Choices.RETURN_BOOK, "ReturnBookAction");
//        actionMap.put(Choices.CHECKOUT_MOVIE, "CheckoutMovieAction");
//        actionMap.put(Choices.RETURN_MOVIE, "ReturnMovieAction");
//        actionMap.put(Choices.LOGOUT, "LogoutAction");
//        actionMap.put(Choices.QUIT, "QuitAction");


        actionMap.put(Choices.SHOW_PROFILE, new ShowProfileAction() );
        actionMap.put(Choices.LIST_CATALOG, new ListCatalogAction());
        actionMap.put(Choices.CHECKOUT_BOOK, new CheckOutBookAction());
        actionMap.put(Choices.RETURN_BOOK, new ReturnBookAction());
        actionMap.put(Choices.CHECKOUT_MOVIE, new CheckoutMovieAction());
        actionMap.put(Choices.RETURN_MOVIE, new ReturnMovieAction());
        actionMap.put(Choices.LOGOUT, new LogoutAction());
        actionMap.put(Choices.QUIT, new QuitAction());
    }

    public static void main(String args[]) throws IOException, InstantiationException, IllegalAccessException, NullPointerException{
        (new LibraryManager()).run();
    }

    public boolean userLoggedIn() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        return this.isLoggedIn;

    }

    public void run() throws IOException, IllegalAccessException, InstantiationException {
        output.welcomeMessage();
        String password;
//        Class actionClass;

        while (true) {
            if (isLoggedIn) {
                output.menuDisplay();
                Choices mainMenuChoice = Choices.values()[getChoice()];
                Action action = actionMap.get(mainMenuChoice);
                ActionParams actionParams=new ActionParams(library, this, userDetails, input, output);
                action.doAction(actionParams);
//                try {
//
//                    String actionClassName = actionMap.get(mainMenuChoice);
//
//                    actionClass = Class.forName(actionClassName);
//
//                    Action action = (Action) actionClass.newInstance();
//
//                    ActionParams actionParams = new ActionParams(library, this, new UserDetails(), new Input(), new Output());
//
//                    action.doAction(actionParams);
//
//                } catch (ClassNotFoundException e) {
//                    output.displayMessage("Invalid choice");
//                }

            } else {
                output.displayMessage("Enter the Library Number");
                libraryNumber = input.getString();
                output.displayMessage("Enter the password");
                password = input.getString();
                userLogin(login, libraryNumber, password);
                if (!this.isLoggedIn)
                    output.displayMessage("Invalid user");
            }
        }
    }

    public void userLogin(HashMap<String, String> login, String libraryNumber, String password) {
        String value = "";

        for (String key : login.keySet())
            if (libraryNumber.equals(key)) {
                value = login.get(key);
                break;
            }

        isLoggedIn = value.equals(password);
    }

    public int getChoice() {
        int choice = input.getInteger();
            return choice;
    }

}

